package com.hanxiaozhang.system.controller;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hanxiaozhang.annotation.EnableGlobalException;
import com.hanxiaozhang.annotation.LogAnnotation;
import com.hanxiaozhang.constant.ModuleTypeEnum;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.system.dao.JobDao;
import com.hanxiaozhang.util.PageUtil;
import com.hanxiaozhang.validation.util.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanxiaozhang.system.entity.JobEntity;
import com.hanxiaozhang.system.service.JobService;

import javax.annotation.Resource;


/**
 * 岗位表
 *
 * @author hanxiaozhang
 * @email hanxiaozhang2018@sina.com
 * @date 2021-03-30
 */
@Slf4j
@Controller
@RequestMapping("/job")
@EnableGlobalException
public class JobController {

    @Autowired
    private JobService jobService;
    @Resource
    private JobDao jobDao;

    /**
     * 查询列表
     *
     * @param params
     * @return
     */
    @LogAnnotation(operation = "岗位列表", moduleName = ModuleTypeEnum.SYSTEM)
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:job:list')")
    public Result<PageUtil> list(@RequestParam Map<String, Object> params) {

        Page<JobEntity> page = PageHelper
                .startPage(Integer.parseInt(params.get("current").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<JobEntity> jobList = jobService.list(params);
        return Result.success(new PageUtil(jobList, (int) page.getTotal()));
    }


    /**
     * 保存
     *
     * @param job
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:job:save')")
    public Result<Void> save(JobEntity job) {
        valid(job, true);
        jobService.save(job);
        return Result.success();
    }


    /**
     * 修改
     *
     * @param job
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('system:job:update')")
    public Result<Void> update(JobEntity job) {
        valid(job, false);
        jobService.update(job);
        return Result.success();
    }


    /**
     * 参数校验
     *
     * @param job
     * @param saveFlag
     */
    private void valid(JobEntity job, boolean saveFlag) {

        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(job.getJobName()), "岗位名称不能为空");
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(job.getJobOrder()), "岗位排序不能为空");
        if (saveFlag) {
            JobEntity oldJob = jobDao.getByJobName(job.getJobName());
            ValidationUtil.checkParameter(oldJob == null, "岗位名称重复");
        } else {
            JobEntity oldJob = jobDao.getByJobNameAndNoJobId(job.getJobName(), job.getJobId());
            ValidationUtil.checkParameter(oldJob == null, "岗位名称重复");
        }
    }

    /**
     * 删除
     *
     * @param jobId
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    @PreAuthorize("hasAuthority('system:job:remove')")
    public Result<Void> remove(Long jobId) {

        return jobService.remove(jobId);
    }


    /**
     * 批量删除
     *
     * @param jobIds
     * @return
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @PreAuthorize("hasAuthority('system:job:batchRemove')")
    public Result<Void> batchRemove(Long[] jobIds) {

        return jobService.batchRemove(jobIds);
    }

}
